//
//  PageTitleView.swift
//  LPZ-DYZB
//
//  Created by apple on 16/11/14.
//  Copyright © 2016年 刘培壮. All rights reserved.
//

import UIKit

private let kLineHeight = 2 as CGFloat

class PageTitleView: UIView {

    fileprivate var titles : [String]
    
    fileprivate lazy var scrollView: UIScrollView = {
        $0.frame = self.bounds
        $0.backgroundColor = .white
        $0.scrollsToTop = false
        $0.bounces = false
        return $0
    }(UIScrollView())
    
    fileprivate lazy var scrollLine: UIView = {
        let view = UIView()
        view.backgroundColor = .theme
        return view
    }()
    
    init(frame: CGRect, titles: [String]) {
        
        self.titles = titles
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


extension PageTitleView {
    
    fileprivate func setUpUI() {

        addSubview(scrollView)
        setUpTitleView()
    }
    
    fileprivate func setUpTitleView() {
        
        let labelW = frame.width / CGFloat(titles.count)
        let labelY = 0 as CGFloat
        let labelH = frame.height - kLineHeight
        for (index, title) in titles.enumerated() {
            let labelX = labelW * CGFloat(index)
            let label = UILabel()
            label.text = title
            label.font = UIFont.systemFont(ofSize: 16)
            label.textAlignment = .center
            label.frame = CGRect(x: labelX, y: labelY, width: labelW, height: labelH)
            scrollView.addSubview(label)
        }
        scrollLine.frame = CGRect(x: 0, y: labelH, width: labelW, height: kLineHeight)
        scrollView.addSubview(scrollLine)
    }
}
